Skip to content

fix: fix count tokens for bedrock models#2254

Merged
mehtarac merged 3 commits intostrands-agents:mainfrom
mehtarac:fix_counttokens
May 6, 2026
Merged

fix: fix count tokens for bedrock models#2254
mehtarac merged 3 commits intostrands-agents:mainfrom
mehtarac:fix_counttokens

Conversation

@mehtarac
Copy link
Copy Markdown
Member

@mehtarac mehtarac commented May 6, 2026

Description

When BedrockModel uses a cross-region inference profile (e.g., us.anthropic.claude-sonnet-4-6), the CountTokens API returns ValidationException. The SDK then falls back to tiktoken, which attempts to download encoding files from the internet. In isolated VPCs (AgentCore, Lambda in private subnets), this download hangs per model call, blocking all agent invocations and causing pipeline timeouts.

The change in this PR modifies the implementation where now the Bedrock token counting fallback path now uses a local heuristic estimator (character-based) instead of tiktoken when the native CountTokens API fails.

Related Issues

Documentation PR

Type of Change

Bug fix
New feature
Breaking change
Documentation update
Other (please describe):

Testing

How have you tested the change? Verify that the changes do not break functionality or introduce warnings in consuming repositories: agents-docs, agents-tools, agents-cli

  • I ran hatch run prepare

Checklist

  • I have read the CONTRIBUTING document
  • I have added any necessary tests that prove my fix is effective or my feature works
  • I have updated the documentation accordingly
  • I have added an appropriate example to the documentation to outline the feature, or no new docs are needed
  • My changes generate no new warnings
  • Any dependent changes have been merged and published

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

@codecov
Copy link
Copy Markdown

codecov Bot commented May 6, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

Comment thread src/strands/models/model.py
@github-actions
Copy link
Copy Markdown

github-actions Bot commented May 6, 2026

Assessment: Comment

The fix correctly addresses the root cause — tiktoken's network-dependent encoding download causing hangs in isolated VPCs. The approach of simplifying to a character-based heuristic is sound, since the Bedrock-native CountTokens API is the primary path and this is only the fallback.

Review Details
  • Stale documentation: Multiple docstrings and comments across model.py and context_offloader/plugin.py still reference tiktoken, which is now fully removed. These should be updated to accurately describe the current behavior.
  • Formatting: Minor PEP 8 issue with a missing blank line before the first top-level function definition.
  • Checklist: The hatch run prepare checkbox is unchecked — please confirm the formatter/linter passes cleanly.

The change is well-scoped and the test updates correctly validate the new heuristic behavior.

@mehtarac mehtarac enabled auto-merge (squash) May 6, 2026 14:29
@mehtarac mehtarac merged commit d94d516 into strands-agents:main May 6, 2026
20 of 21 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants